package com.example.mvp.presenter;

import com.example.mvp.model.BaseEntity;
import com.example.mvp.model.IModel;
import com.example.mvp.view.IView;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract class BasePresenter<M extends IModel, V extends IView> implements IPresenter, Observer<BaseEntity> {

    protected M mModel;
    protected V mView;
    protected CompositeDisposable compositeDisposable;

    public BasePresenter(M mModel, V mView) {
        this.mModel = mModel;
        this.mView = mView;
        compositeDisposable = new CompositeDisposable();
    }

    @Override
    public void onDestroy() {
        compositeDisposable.dispose();
        compositeDisposable.clear();
    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {
        compositeDisposable.add(d);
        mView.showDialog();
    }

    @Override
    public void onError(@NonNull Throwable e) {
        mView.showToast(e.getMessage());
    }

    @Override
    public void onComplete() {
        mView.hideDialog();
    }
}
